﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weikebuluo.Model;
using weikebuluo.DAL;

namespace weikeweixin.web.admin.website
{
    public partial class add : weikeweixin.web.admin.cs.adminbase
    {
        t_icon_model HomeIconModel = new t_icon_model();
        t_icon_dal HomeIconDAL = new t_icon_dal();
        protected string PicUrl = "";
        protected string PicUrl_Status = "";
        protected string Status = "";
        protected string BgColor = "";
        protected string Name = "";
        protected string Name_Status = "";
        protected string Sort = "0";
        protected string Type = "link";
        protected string Href = "";
        protected string title = "添加首页菜单";
        protected string Opacity = "";
        protected string Color = "";
        protected string BgImg = "";
        protected string BgImg_Status = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(user_id))
            {
                Response.Write("<script Language=\"Javascript\">alert(\"请先配置微官网\")");
                Response.Write("</script>");
                Response.End();
            }
            if (!IsPostBack&&Request.QueryString["id"] != null)
            {
                title = " 修改首页菜单";
                System.Data.DataTable dt = HomeIconDAL.GetList("Id=" + Request.QueryString["id"]).Tables[0];
                PicUrl = dt.Rows[0]["PicUrl"].ToString();
                PicUrl_Status = dt.Rows[0]["PicUrl_Status"].ToString(); 
                 Status = dt.Rows[0]["Status"].ToString();
                 BgColor = dt.Rows[0]["BgColor"].ToString();
                 Name = dt.Rows[0]["Name"].ToString();
                 Name_Status = dt.Rows[0]["Name_Status"].ToString();
                 Sort = dt.Rows[0]["Sort"].ToString();
                 Type = dt.Rows[0]["Type2"].ToString();
                 Href = dt.Rows[0]["Href"].ToString();
                 Opacity = dt.Rows[0]["Opacity"].ToString();
                 Color = dt.Rows[0]["Color"].ToString();
                 BgImg = dt.Rows[0]["BgImg"].ToString();
                 BgImg_Status = dt.Rows[0]["BgImg_Status"].ToString();
            }
            switch (Request.Form["action"])
            {
                case "save":
                    Save();
                    break;
            }
        }

        private void Save()
        {
            
            string str = Request.Form["cbclassname"];
            HomeIconModel.PicUrl = Request.Form["icon"];
            HomeIconModel.PicUrl_Status = bool.Parse(Request.Form["cbIcon"]);
            HomeIconModel.Status = int.Parse(string.IsNullOrEmpty(Request.Form["is_home"]) ? "0" : Request.Form["is_home"]);
            HomeIconModel.BgColor = Request.Form["BgColor"];
            HomeIconModel.user_id = int.Parse(user_id);
            HomeIconModel.Name = Request.Form["classname"];
            HomeIconModel.Name_Status = bool.Parse(Request.Form["cbclassname"]);
            HomeIconModel.Sort = int.Parse(string.IsNullOrEmpty(Request.Form["sort"]) ? "0" : Request.Form["sort"]);
            HomeIconModel.Type2 = Request.Form["type"];
            HomeIconModel.Opacity = float.Parse(string.IsNullOrEmpty(Request.Form["Opacity"])?"1":Request.Form["Opacity"]);
            HomeIconModel.Color = Request.Form["Color"];
            HomeIconModel.BgImg = Request.Form["picurl"];
            HomeIconModel.BgImg_Status = bool.Parse(Request.Form["cbBgImg"]);
            string url = Request.Url.Authority;
            switch(HomeIconModel.Type2)
            {
                case "articlelist":
                    HomeIconModel.Href = "http://" + url + "/phone/List?userid=" +user_id+ "&cid=" + Request.Form["category_id"];
                    HomeIconModel.Type = "Default";
                    break;
                case "article":
                    if (string.IsNullOrEmpty(Request.Form["article"]))
                    {
                        Response.Write("{\"errno\":\"1\",\"tip\":\"请选择图文！\",\"url\":\"homeadd.aspx\",\"error\":\"请选择图文！\"}");
                        Response.End();
                    }
                    else
                    {
                        HomeIconModel.Href = "http://" + url + "/phone/News?userid=" + user_id + "&nid=" + Request.Form["article"];
                        HomeIconModel.Type = "Default";
                    }
                    break;
                case "link":
                    HomeIconModel.Href = Request.Form["link"];
                    HomeIconModel.Type = "Default";
                    break;
                case "tel":
                    HomeIconModel.Href = Request.Form["tel"];
                    HomeIconModel.Type = "Phone";
                    break;
            }
            if (string.IsNullOrEmpty(Request.Form["id"]))
            {
                int re= HomeIconDAL.Add(HomeIconModel);
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"home.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"homeadd.aspx\",\"error\":\"\"}");
                }
            }
            else
            {
                HomeIconModel.Id = int.Parse(Request.Form["id"].Trim());
                bool re = HomeIconDAL.Update(HomeIconModel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"home.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"homeadd.aspx\",\"error\":\"\"}");
                }
            }
            Response.End();
        }
    }
}